package valuetypes;

import rdbms.Entry;
import rdbms.Group;
import rdbms.ddl.Field;

public class GroupValue extends DBValue {

	Group group;
	Entry entry;
	DBValue[] subValues;
	
	public GroupValue(Group group, Entry entry) {
		super(sizeSum(group, entry));			
		this.group = group;
		this.entry = entry;		
		Field[] fields = group.getFields();
		this.subValues = new DBValue[fields.length];
		for(int i = 0; i < fields.length; i ++){
			subValues[i] = entry.getValue(fields[i]);
		}
	}

	private static int sizeSum(Group group, Entry entry) {
		int size = 0;
		if(group == null || entry == null){
			System.out.println("BOOOOOM");
		}
		for(Field f : group.getFields()){
			size += entry.getValue(f).getSize();
		}
		return size;
	}
	
	public DBValue[] getSubValues() {
		return subValues;
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof GroupValue){
			GroupValue gv = (GroupValue)obj;
			if(gv.subValues.length != this.subValues.length)return false;
			for(int i = 0; i < subValues.length; i ++){
				if(!subValues[i].equals(gv.subValues[i]))return false;
			}
			return true;
		}
		return false;
	}
	
	@Override
	public String toString() {
		String s = "{ ";
		boolean first = true;
		for(DBValue value : subValues){
			if(first)first = false;
			else s += " | ";
			s += value.toString();
		}
		return s + " }";
	}

}
